package com.example.aigc_education.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.aigc_education.anno.RequestKeyParam;
import com.example.aigc_education.anno.RequestLock;
import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.domain.NoteTag;
import com.example.aigc_education.domain.dto.NoteDTO;
import com.example.aigc_education.domain.vo.NoteTagVO;
import com.example.aigc_education.domain.vo.NoteVO;
import com.example.aigc_education.mapper.NoteTagMapper;
import com.example.aigc_education.service.NoteService;
import com.example.aigc_education.service.NoteTagService;
import com.example.aigc_education.utils.ResultUtil;
import com.example.aigc_education.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/note")
public class NoteController {

    @Resource
    private NoteService noteService;

    @Resource
    private NoteTagService noteTagService;

    @Resource
    private NoteTagMapper noteTagMapper;


    /**
     * 添加笔记
     * @param noteDTO 笔记DTO对象
     * @return 操作成功信息
     */
    @RequestLock(prefix = "addNote", expire = 3)
    @PostMapping("/addNote")
    public ResultUtil addNote(@RequestKeyParam @RequestBody NoteDTO noteDTO){

        Long noteId = noteService.addNote(noteDTO);
        return ResultUtil.success(noteId);
    }

    /**
     * 通过themeId定位搜索该计划下所有的笔记事件
     *
     * @param themeId   笔记所属计划id
     * @return      该计划下的所有的note集合
     */
    @GetMapping("/noteList")
    public ResultUtil noteList(Long themeId){
        List<NoteVO> notes = noteService.noteList(themeId);

        // 返回操作成功的ResultUtil对象
        return ResultUtil.success(notes);
    }


    /**
     * 保存笔记
     * @param noteDTO 笔记DTO对象
     * @return 操作成功信息
     */
    @RequestLock(prefix = "saveNote", expire = 3)
    @PutMapping("/saveNote")
    public ResultUtil updateNote(@RequestKeyParam @RequestBody NoteDTO noteDTO){

        noteService.updateNote(noteDTO);
        return ResultUtil.success("保存成功");
    }


    /**
     * 删除笔记事件
     * @param noteId    待删除的笔记的id
     * @return      操作成功、失败信息
     */
    @RequestLock(prefix = "deleteNote", expire = 3)
    @DeleteMapping("/deleteNote")
    public ResultUtil deleteNote(@RequestKeyParam Long noteId){
        noteService.deleteById(noteId);
        return ResultUtil.success("笔记删除成功");
    }


    /**
     * 根据 noteId 查询笔记，并且笔记按照创建的时间降序排序
     * @param noteId     笔记的标题
     * @return              笔记的集合
     */
    @GetMapping("/queryNote")
    public ResultUtil queryNote(Long noteId){
        List<NoteVO> notes = noteService.queryByNoteId(noteId);

        return ResultUtil.success(notes);
    }

    /**
     * 根据标签名查询笔记
     * @param tagName 标签名
     * @return 笔记集合
     */
    @GetMapping("/getNoteByTag/{tagName}")
    public ResultUtil getNoteByTagName( @PathVariable("tagName") String tagName){
        Long userId = SecurityUtils.getUserId();
        List<NoteVO> notes = noteTagService.selectNoteByTagName(userId, tagName);
        return ResultUtil.success(notes);
    }

    /**
     * 根据用户id查询所有标签
     * @return 标签集合
     */
    @GetMapping("/getTags")
    public ResultUtil getTagsByUserId(){
        Long userId = SecurityUtils.getUserId();
        List<String> noteTagVOS = noteTagService.selectTagsByUserId(userId);
        return ResultUtil.success(noteTagVOS);
    }

    /**
     * 获取当前用户所有笔记
     * @return  操作成功信息
     */
    @GetMapping("/getAllNote")
    public ResultUtil getAllNoteByUserId(){
        Long userId = SecurityUtils.getUserId();
        List<NoteVO> noteVOS = noteService.selectNoteByUserId(userId);

        return ResultUtil.success(noteVOS);
    }


    /**
     * 根据tagName删除tag
     * @param tagName   tag名字
     * @return          操作成功信息
     */
    @RequestLock(prefix = "deleteTag", expire = 3)
    @DeleteMapping("/deleteTag")
    public ResultUtil deleteTag(@RequestKeyParam  String tagName){
        try {
            LambdaQueryWrapper<NoteTag> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NoteTag::getTagName, tagName);
            noteTagMapper.delete(queryWrapper);

            return ResultUtil.success("删除成功");
        }catch (Exception e){
            throw new BusinessException("删除tag异常");
        }
    }

    /**
     * 删除笔记标签
     * @param noteId    笔记id
     * @param tagName   标签名字
     * @return          操作成功信息
     */
    @RequestLock(prefix = "deleteNoteTag", expire = 3)
    @Transactional
    @DeleteMapping("/deleteNoteTag")
    public ResultUtil deleteNoteTag(@RequestKeyParam Long noteId,String tagName){
        try{

            noteTagMapper.deleteNoteTag(noteId, tagName);
            return ResultUtil.success("删除成功");

        }catch (Exception e){
            throw new BusinessException("删除笔记标签异常");
        }
    }
}
